package com.nec.recommend.config;

import com.nec.recommend.auth.AuthProp;
import com.nec.recommend.interceptor.GlobalAuthInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class AuthInterceptorConfig implements WebMvcConfigurer {
    private final AuthProp authProp;
    private final GlobalAuthInterceptor globalAuthInterceptor;

    public AuthInterceptorConfig(AuthProp authProp, GlobalAuthInterceptor globalAuthInterceptor) {
        this.authProp = authProp;
        this.globalAuthInterceptor = globalAuthInterceptor;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/").addResourceLocations("classpath:/META-INF/resources/webjars/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(globalAuthInterceptor).addPathPatterns("/**").excludePathPatterns(authProp.getUrls());
    }
}
